// components/subscribe/cmp.js
import { SubscribeModal } from '../../model/subscribe'
const subscribeModal = new SubscribeModal()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: String,
    tagId: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    class: 'common',
    myTagList: []
  },

  attached() {
    this.judgeTag()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onTap() {

      const mark = {
        type: this.properties.type,
        tagId: this.properties.tagId
      }

      if (this.data.class === 'common') {
        const myTagList = this.getMyTagList()
        myTagList.push(mark)
        subscribeModal.setMyTagList(myTagList)
      } else {
        subscribeModal.removeMyTagList(mark.tagId)
      }


      this._toggleClass()
      this.triggerEvent('tap')
    },
    getMyTagList() {
      const myTagList = subscribeModal.getMyTagList()
      this.setData({
        myTagList
      })
      return myTagList
    },
    judgeTag() {
      const myTagList = this.getMyTagList()
      myTagList.forEach((item, index) => {
        if (item.tagId === this.properties.tagId) {
          this.setData({
            class: 'subscribe'
          })
        }
      })
    },
    _toggleClass() {
      let className = ''
      if (this.data.class == 'common') {
        className = 'subscribe'
      } else {
        className = 'common'
      }
      this.setData({
        class: className
      })
    }
  }
})
